package com.cema.manage.project.android.biz;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.common.exception.BizException;
import com.cema.manage.project.android.TestBaseContrller;
import com.cema.manage.project.android.result.JsonResult;
import com.cema.manage.project.manage.proCollect.domain.ProCollect;
import com.cema.manage.project.manage.proCollect.service.IProCollectService;
import com.cema.manage.project.manage.proGoods.domain.ProGoods;
import com.cema.manage.project.manage.proGoods.domain.ProGoodsVo;
import com.cema.manage.project.manage.proGoods.service.IProGoodsService;
import com.cema.manage.project.manage.proLike.domain.ProLike;
import com.cema.manage.project.manage.proLike.service.IProLikeService;
import com.cema.manage.project.manage.proOrder.domain.ProOrder;
import com.cema.manage.project.manage.proOrder.service.IProOrderService;
import com.cema.manage.project.manage.users.domain.Users;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@RequestMapping("pro_user")
@RestController
public class BizProUserController extends TestBaseContrller {

    @Resource
    private IProCollectService iProCollectService;

    @Resource
    private IProLikeService iProLikeService;

    @Resource
    private IProGoodsService iProGoodsService;

    @Resource
    private IProOrderService iProOrderService;

    @PostMapping("order")
    public JsonResult createOrder(HttpServletRequest request, ProCollect proCollect) {
        Users users = getUser(request);
        try {
            iProOrderService.createOrder(users.getId(), proCollect.getPgId());
            return JsonResult.returnSuccess("成功");
        } catch (BizException e) {
            return JsonResult.returnFail(e.getMessage());
        }
    }

    @PostMapping("collect")
    public JsonResult getCollect(HttpServletRequest request, ProCollect proCollect) {
        Users users = getUser(request);
        EntityWrapper<ProCollect> entityWrapper = new EntityWrapper<>();
        proCollect.setUsId(users.getId());
        entityWrapper.setEntity(proCollect);
        ProCollect collect = iProCollectService.selectOne(entityWrapper);
        String msg = "收藏成功";
        if (ObjectUtil.isNotNull(collect)) {
            iProCollectService.deleteById(collect.getId());
            msg = "取消收藏";
        } else {
            iProCollectService.insert(proCollect);
        }
        return JsonResult.returnSuccess(msg);
    }

    @PostMapping("like")
    public JsonResult getLike(HttpServletRequest request, ProLike proLike) {
        Users users = getUser(request);
        EntityWrapper<ProLike> entityWrapper = new EntityWrapper<>();
        proLike.setUsId(users.getId());
        entityWrapper.setEntity(proLike);
        ProLike like = iProLikeService.selectOne(entityWrapper);
        String msg = "点赞成功";
        if (ObjectUtil.isNotNull(like)) {
            iProLikeService.deleteById(like.getId());
            msg = "取消点赞";
        } else {
            iProLikeService.insert(proLike);
        }
        return JsonResult.returnSuccess(msg);
    }

    @PostMapping("goods")
    public JsonResult getGoodsList(HttpServletRequest request) {
        Users users = getUser(request);
        EntityWrapper<ProCollect> collectEntityWrapper = new EntityWrapper<>();
        collectEntityWrapper.eq("us_id", users.getId());
        List<ProCollect> collectList = iProCollectService.selectList(collectEntityWrapper);
        if (ArrayUtil.isEmpty(collectList) && collectList.size() > 0) {
            return JsonResult.returnResult(JsonResult.SUCCESS, new ArrayList<>());
        }
        List<Integer> pgIdList = collectList.stream().map(ProCollect::getPgId).collect(Collectors.toList());
        EntityWrapper<ProGoods> goodsEntityWrapper = new EntityWrapper<>();
        goodsEntityWrapper.in("id", pgIdList);
        List<ProGoods> goodsList = iProGoodsService.selectList(goodsEntityWrapper);
        return JsonResult.returnResult(JsonResult.SUCCESS, goodsList);
    }
}
